package com.example.gametestproject;

import android.graphics.Canvas;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import com.example.gametestproject.GameView;

public class RenderThread extends Thread {
	
	//control frame rates
	public final static int MAX_FPS = 40;
	public final static int MAN_FRAME_SKIP = 5;
	public final static long FRAME_PERIOD = 1000 / MAX_FPS;
	
	public int mFrameSkipped;
	private SurfaceHolder mSurfaceHolder;
	private GameView mGameView;
	//private Handler mHandler;
	private boolean mIsRunning;
	private boolean needUpdate;
	
	
	public RenderThread(SurfaceHolder iSurfaceHolder, GameView iGameView, Handler iHandler)
	{
		this.mSurfaceHolder = iSurfaceHolder;
		this.mGameView = iGameView;
		//this.mHandler = iHandler;
		
		this.mFrameSkipped = 0;
		this.mIsRunning = true;
		this.needUpdate = true;
		
	}

	public boolean isRunning()
	{
		return this.needUpdate;
	}
	public void setRunning(boolean running)
	{
			this.needUpdate = running;
	}
	
	@Override 
	//responsible for update screen and control fps
	public void run()
	{
		Canvas canvas = null;
		while (this.mIsRunning)
		{

            if (this.mSurfaceHolder.getSurface().isValid() && this.needUpdate)
            {
                 long iterStartTime = System.currentTimeMillis();
                try
                {
                    long timeConsume = 0;
                    //Log.i("update", "update start!");
                    canvas = this.mSurfaceHolder.lockCanvas();
                    synchronized(this.mSurfaceHolder)
                    {
                        long lRenderTimeStart = System.currentTimeMillis();
                        //update the surfaceView here
                       // if(this.needUpdate)
                        {
                            this.mGameView.update();
                            this.mGameView.paint(canvas);
                        }
                        timeConsume = System.currentTimeMillis() - lRenderTimeStart;
                        //Log.i("render", "frame use time " + timeConsume + "ms");
                    }

                    long sleepTime = FRAME_PERIOD - timeConsume;

                    if (sleepTime<0)
                    {
                        this.mGameView.update();//skip one frame
                        //Log.i("render", "delay time " + sleepTime + "ms");
                        sleepTime += FRAME_PERIOD;
                    }
                    else
                    {
                        //Log.i("render", "sleep time " + sleepTime + "ms");
                        Thread.sleep(sleepTime);
                    }
                }
                catch (InterruptedException e)
                {
                    System.out.println("InterruptedException " + e.getMessage());
                }
                catch (Exception e)
                {
                    System.out.println("Exception in run of RenderThread " + e.getMessage());
                }
                finally
                {
                    if(canvas != null)
                        this.mSurfaceHolder.unlockCanvasAndPost(canvas);
                }

                long iterEndTime = System.currentTimeMillis();
                this.mGameView.getFPSSprite().updateFPS((int)(1000 / Math.max(1, (iterEndTime - iterStartTime))));

            }

		}
	}
	
	
}
